package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.relatives.RelativesMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BUSY_FARMING_FRIEND_HOME_DETAIL,desc = "亲家果园")
public class BusyFarmingFriendHomeDetailCmd extends AbstractCommand {

    long userId;
    long userServerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.BusyFarmingFriendHomeDetailReqMsg reqMsg = BusyFarmingProto.BusyFarmingFriendHomeDetailReqMsg.parseFrom(packet.getBytes());
        userId = reqMsg.getUserId();
        if(reqMsg.hasServerId()){
            userServerId = reqMsg.getServerId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BusyFarmingMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BusyFarmingMgr.activityInTime()){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_IN_TIME;
            return;
        }
        if(userServerId == 0){
            //从商会拿数据
            CrossUnionMember crossUnionMember = CrossUnionMgr.getUnionMember(player.getUserInfo().getUnionUid(), userId);
            if(crossUnionMember != null){
                userServerId = crossUnionMember.getServerId();
            }
        }

        if(userServerId == 0){
            userServerId = RelativesMgr.getUserServerId(userId);
        }

        if(userServerId == GameServer.getInstance().getServerId() || userServerId == 0) {
            ret = player.getModule(BusyFarmingModule.class).friendHomeDetail(userId);
        }else {
            player.getModule(BusyFarmingModule.class).crossFriendHomeDetail(userServerId, userId);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_BUSY_FARMING_FRIEND_HOME_DETAIL, BusyFarmingProto.BusyFarmingFriendHomeDetailRespMsg.newBuilder().setRet(ret));
        }
    }
}
